/*
 *说明：用户凭证申请管理脚本
 *
 *@author liuxun 2012-03-17
*/

var opeForm;
var grid;

$(function(){
	opeForm = $('#opeForm');

	grid=$('#listTable').datagrid({
		frozenColumns:[[
            {title:'选择',field:'ck',checkbox:true}
		]],
		columns:[[

            {title:'统一登录名',field:'LOGIN_NAME',align:'center',width:80},

            {title:'用户姓名',field:'USR_NAME',align:'center',width:80},

            {title:'所属机构',field:'ORG_NAME',align:'center',width:180},

            {title:'用户类型',field:'USR_TYPE',align:'center',width:80},
            
            {title:'认证方式',field:'AUTH_MODE_DECODE',align:'center',width:90},

            {title:'凭证编号',field:'CREDENCE_ID',align:'center',width:80},

            {title:'操作员',field:'IS_OPERATOR',align:'center',width:80, 
            	formatter:function(value,rec){
          	       return value == '1' ? "<font color='green'>√</font>" : "";
 			    }
            },

            {title:'用户状态',field:'USR_STATUS_DECODE',align:'center',width:80},
            
            {title:'凭证状态',field:'CREDENCE_STATUS',align:'center',width:80}

        ]],
		pagination:true,
		singleSelect: false
	});

	queryOperation();

	$('#queryBtn').click(queryOperation);
	$('#clearBtn').click(clearOperation);
	$('#reloadBtn').click(refreshWin);
	$('#applyStaticPwdBtn').click(applyStaticPwdOperation);
	$('#applyLockTokenBtn').click(applyDynamicTokenLockOperation);
	$('#applyDeblockingTokenBtn').click(applyDynamicTokenDeblockingOperation);
	$('#applyDeleteTokenBtn').click(applyDynamicTokenDeleteOperation);
	
});

//翻页时自动调用此函数
function onLoadPrev(msg){
	var queryParams=grid.datagrid('options').queryParams;
	queryParams['firstQuery']=false;
}

//查询操作
function queryOperation(){
	var params = opeForm.serializeObject();
	grid.datagrid("options").pageNumber = 1;
	grid.datagrid({
		url: 'B_USR_CREDENCE!queryB_USR_CREDENCE_MANAGERList.do',
		queryParams: jQuery.extend({}, params, {'firstQuery': true})
	});
}

//清空操作（重置）
function clearOperation() {
	opeForm.resetForm();
}

//选择机构
function select_a_org() {
	var action = "A_ORG!showTree.do";
	var row = showModalWin(action);
	
	if (row) {
		$('#org_sequence').val(row.attributes.org_sequence);
		$('#org_name').val(row.attributes.org_name);
	}
	
}
//查看明细
function showInfo(key) {
	var url = 'A_USR!showInfo.do?model.key=' + key;
	openWin(url,'pop'); 
}

//静态口令重置
function applyStaticPwdOperation(){  
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中一条记录!');
		return;
	}  

	if (rows.length > 1) {
		showInfoWin('只能选择一条记录');
      	return; 
	}
	
	var usr_status = rows[0].USR_STATUS;
	var auth_mode = rows[0].AUTH_MODE;
	
	if(usr_status == '03'){
		showInfoWin('该用户已经删除，不允许重置静态口令！');
      	return;
	}
	
	if(auth_mode != 'StaticPwd'){
		showInfoWin('非静态口令，不允许重置操作!');
      	return;
	}
		
	var url = 'B_USR_CREDENCE!showApplyStaticPwdReSet.do?model.usr_id=' + rows[0].USR_ID+'&model.is_static_create_or_update=1';
	openWin(url,'pop');
}

//动态令牌锁定
function applyDynamicTokenLockOperation(){  
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中要锁定的记录!');	
		return;
	}

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		var auth_mode = rows[i].AUTH_MODE;
		if(auth_mode == 'StaticPwd'){
			showInfoWin('非动态令牌，不允许锁定操作!');
	      	return;
		}
		ids.push(rows[i].AUTO_ID);
	}
	var params = $ .param({'id' : ids}, true);

	$ .messager.confirm('提示信息', '您确定要锁定选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'B_USR_CREDENCE!doApplyDynamicTokenLock.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

//动态令牌解锁
function applyDynamicTokenDeblockingOperation(){  
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中要解锁的记录!');	
		return;
	}

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		var auth_mode = rows[i].AUTH_MODE;
		if(auth_mode == 'StaticPwd'){
			showInfoWin('非动态令牌，不允许解锁操作!');
	      	return;
		}
		ids.push(rows[i].AUTO_ID);
	}
	var params = $ .param({'id' : ids}, true);

	$ .messager.confirm('提示信息', '您确定要解锁选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'B_USR_CREDENCE!doApplyDynamicTokenDeblocking.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

//动态令牌解绑
function applyDynamicTokenDeleteOperation(){  
	var rows = grid.datagrid('getSelections');

	if (rows.length == 0) {
		showInfoWin('请您首先选中要删除的记录!');	
		return;
	}

	var ids = [];
	for (var i = 0; i < rows.length; i++) {
		var auth_mode = rows[i].AUTH_MODE;
		if(auth_mode == 'StaticPwd'){
			showInfoWin('非动态令牌，不允许解绑操作!');
	      	return;
		}
		ids.push(rows[i].AUTO_ID);
	}
	var params = $ .param({'id' : ids}, true);

	$ .messager.confirm('提示信息', '您确定要解绑选定的' + rows.length + '条记录吗?', function(result){
		if(result){
			$ .ajax({
				url: 'B_USR_CREDENCE!doDelete.do',
				data: params,
				type: 'post',
				success: function(data){
				    showInfoWin('操作成功!');
					afterOK();
				}
			});
		}	
	});
}

//操作成功后刷新列表
function afterOK(){
	queryOperation();
}